package com.bilgeadam.customtags;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author Bora SAYINER
 * @since Jan 18, 2015 5:01:03 PM
 * @version 1.0
 */
public class ForEachTag extends SimpleTagSupport {

	private List items;
	private String var;
	private int begin;
	private int end;
	private int step;

	public ForEachTag() {
		this.begin = 0;
		this.step = 1;
	}

	@Override
	public void doTag() throws JspException, IOException {
		PageContext pageContext = (PageContext) getJspContext();
		for (int i = begin; i < end; i += step) {
			Object item = items.get(i);
			pageContext.setAttribute(var, item);
			getJspBody().invoke(null);
		}
	}

	public List getItems() {
		return items;
	}

	public void setItems(List items) {
		this.items = items;
		this.end = items.size();
	}

	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}

	public int getBegin() {
		return begin;
	}

	public void setBegin(int begin) {
		this.begin = begin;
	}

	public int getEnd() {
		return end;
	}

	public void setEnd(int end) {
		this.end = end;
	}

	public int getStep() {
		return step;
	}

	public void setStep(int step) {
		this.step = step;
	}

}
